/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.SalesDocumentType;
import org.crystalrange.enterprise.service.SalesDocumentTypeService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class SalesDocumentTypeListController extends BaseController {

    protected SalesDocumentTypeService salesDocumentTypeService;
    protected SalesDocumentType selectedSalesDocumentType;
    protected Listbox listboxSalesDocumentType;
    protected Search searchSalesDocumentType;
    protected List<SalesDocumentType> salesDocumentTypes;

    public SalesDocumentTypeService getSalesDocumentTypeService() {
        return this.salesDocumentTypeService;
    }

    public void setSalesDocumentTypeService(SalesDocumentTypeService salesDocumentTypeService) {
        this.salesDocumentTypeService = salesDocumentTypeService;
    }

    public SalesDocumentType getSelectedSalesDocumentType() {
        return this.selectedSalesDocumentType;
    }

    public void setSelectedSalesDocumentType(SalesDocumentType selectedSalesDocumentType) {
        this.selectedSalesDocumentType = selectedSalesDocumentType;
    }

    public List<SalesDocumentType> getSalesDocumentTypes() {
        return this.salesDocumentTypes;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchSalesDocumentType = new Search(SalesDocumentType.class);
        this.searchSalesDocumentType.setMaxResults(100);
        this.searchSalesDocumentType.addFetch("createdBy");
        this.searchSalesDocumentType.addFetch("updatedBy");
        this.salesDocumentTypes = this.salesDocumentTypeService.search(this.searchSalesDocumentType);

        component.setAttribute("controller", this, false);
    }

    public void onClick$menuitemSalesDocumentTypeCreate(Event event) throws Exception {
        this.salesDocumentTypeCreate();
    }

    public void onClick$menuitemSalesDocumentTypeEdit(Event event) throws Exception {
        this.salesDocumentTypeEdit();
    }

    public void onClick$menuitemSalesDocumentTypeDelete(Event event) throws Exception {
        this.salesDocumentTypeDelete();
    }

    public void onClick$menuitemSalesDocumentTypeSearch(Event event) throws Exception {
        this.salesDocumentTypeSearch();
    }

    public void onClick$menuitemSalesDocumentTypeRefresh(Event event) throws Exception {
        this.salesDocumentTypeRefresh();
    }

    public void onClick$toolbarbuttonSalesDocumentTypeCreate(Event event) throws Exception {
        this.salesDocumentTypeCreate();
    }

    public void onClick$toolbarbuttonSalesDocumentTypeEdit(Event event) throws Exception {
        this.salesDocumentTypeEdit();
    }

    public void onClick$toolbarbuttonSalesDocumentTypeDelete(Event event) throws Exception {
        this.salesDocumentTypeDelete();
    }

    public void onClick$toolbarbuttonSalesDocumentTypeSearch(Event event) throws Exception {
        this.salesDocumentTypeSearch();
    }

    public void onClick$toolbarbuttonSalesDocumentTypeRefresh(Event event) throws Exception {
        this.salesDocumentTypeRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.salesDocumentTypeEdit();
    }
    
    private void salesDocumentTypeCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/salesDocumentType.zul");
    }

    private void salesDocumentTypeEdit() throws Exception {
        if (this.selectedSalesDocumentType != null) {
            this.execution.sendRedirect("~./zul/html/salesDocumentType.zul?Id=" + this.selectedSalesDocumentType.getId().toString());
        }
    }

    private void salesDocumentTypeDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxSalesDocumentType.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    SalesDocumentType salesDocumentType = (SalesDocumentType) selectedItem.getValue();
                    this.salesDocumentTypeService.remove(salesDocumentType);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.salesDocumentTypes = this.salesDocumentTypeService.search(this.searchSalesDocumentType);
        this.binder.loadAll();
    }

    private void salesDocumentTypeSearch() throws Exception {
        this.execution.setAttribute("searchSalesDocumentType", this.searchSalesDocumentType);

        Window window = (Window) this.execution.createComponents("~./zul/html/salesDocumentTypeSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchSalesDocumentType = (Search) this.execution.getAttribute("searchSalesDocumentType");
        }

        this.salesDocumentTypes = this.salesDocumentTypeService.search(this.searchSalesDocumentType);
        this.binder.loadAll();
    }

    private void salesDocumentTypeRefresh() {
        this.salesDocumentTypes = this.salesDocumentTypeService.search(this.searchSalesDocumentType);
        this.binder.loadAll();
    }
}
